package com.zelin.servlet;

import cn.hutool.core.util.StrUtil;
import com.zelin.pojo.Classes;
import com.zelin.pojo.Student;
import com.zelin.service.ClassService;
import com.zelin.service.StudentService;
import com.zelin.service.impl.ClassServiceImpl;
import com.zelin.service.impl.StudentServiceImpl;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @Auther zxh
 * @Date 2020/9/28下午 4:36
 */
@WebServlet("/student")
public class StudentServlet extends HttpServlet {
    private StudentService studentService = new StudentServiceImpl();
    private ClassService classService = new ClassServiceImpl();

    @SneakyThrows
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理post请求的中文乱码问题
        req.setCharacterEncoding("UTF-8");

        //1.得到请求参数
        String cmd = req.getParameter("cmd");
        //2.判断是否存在，如果存在就调用不同的处理方法
        if(StrUtil.isNotBlank(cmd)){
            if("list".equals(cmd)){             //列表学生
                list(req,resp);
            }else if("toAdd".equals(cmd)){        //添加学生
                toAdd(req,resp);
            }else if("add".equals(cmd)){        //添加学生
                add(req,resp);
            }else if("toUpdate".equals(cmd)){   //到修改页面
                toUpdate(req,resp);
            }else if("update".equals(cmd)){     //修改学生
                update(req,resp);
            }else if("delete".equals(cmd)){     //删除学生
                delete(req,resp);
            }
        }


    }

    private void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Classes> list = null;
        try {
            list = classService.find();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        req.setAttribute("list",list);
        req.getRequestDispatcher("/WEB-INF/student/add.jsp").forward(req,resp);
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) {
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
    }

    private void toUpdate(HttpServletRequest req, HttpServletResponse resp) {
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException {
        String stuno = req.getParameter("stuno");
        String stuname = req.getParameter("stuname");
        String classno = req.getParameter("classno");
        String sex = req.getParameter("sex");
        String age = req.getParameter("age");
    System.out.println("stuno:"+stuno);
        Student student = new Student(stuno, stuname, classno, sex, new Integer(age));
    System.out.println("student:"+student);
    studentService.insert(student);
    resp.sendRedirect(req.getContextPath() + "student?cmd=list");
    }



    /**
     * 查询学生
     * @param req
     * @param resp
     * @throws SQLException
     * @throws ServletException
     * @throws IOException
     */
    private void list(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
        List<Student> list = studentService.query();

        //放到请求域中
        req.setAttribute("list",list);
        //转发到页面
        req.getRequestDispatcher("/WEB-INF/student/list.jsp").forward(req,resp);
    }
}
